function getCountArticle() {
    var value = "";
    $.ajax({
        url: 'control/countArticle.php',
        type: 'get',
        dataType: 'text',
        async: false,
        success: function (result) {
            value = result;
        }
    })
    return value;
}

function getContent() {
    var articles = document.getElementById("article_box");
    var count = getCountArticle();
    var html = '';

    for (var i = count; i >= 1; i--) {
        var jsonData;
        $.ajax({
            url: 'control/article.php',
            type: 'post',
            data: {
                "id": i
            },
            dataType: 'json',
            async: false,
            success: function (result) {
                jsonData = eval(result);
            }
        });
        if (JSON.stringify(jsonData) == "{}") {
            continue;
        } else {
            html += '<article class="article">';
            html += '<div>';
            html += '<div>';
            html += '<a href="article.html?id=' + jsonData.id + '">' + jsonData.title + '</a>';
            html += '<p>' + jsonData.time + '</p>';
            html += '</div>';
            html += '<div id="content"><pre style="white-space: pre-line;">' + jsonData.content + '</pre></div>';
            html += '</div>';
            html += '</article>';
            articles.innerHTML = html;
        }
    }
}

function getCategorys() {
    var category = document.getElementById("category");
    var html = '';
    $.ajax({
        url: 'http://localhost/Myblog/view/control/getCategorys.php',
        type: 'get',
        success: function (result) {
            var data = eval(result);
            var count = data.length;

            for (var i = 0; i < count; i++) {
                html += '<a href="javascript:" onclick="getTitleList(&quot;' + data[i].category + '&quot; )">' + data[i].category + '</a>';
            }
            category.innerHTML = html;
        }
    })
}

/**
 * 以下JS函数用于获取url参数
 * @param {String} variable     请求的参数名 
 */
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}

function getTitleList(inputCategory) {
    var articleBox = document.getElementById("article_box");
    $.ajax({
        url: 'http://localhost/Myblog/view/control/getTitleList.php',
        type: 'post',
        data: {
            "category": inputCategory
        },
        dataType: 'json',
        success: function (result) {
            var data = eval(result);
            var count = data.length;
            var html = '';
            html += '<div id="list_box">';
            for (var i = count - 1; i >= 0; i--) {
                html += '<div>';
                html += '<a href="article.html?id=' + data[i].id + '">';
                html += data[i].title + '</a><br />';
                html += '<span>' + data[i].time + '</span>';
                html += '</div>';
            }
            html += '</div>';
            articleBox.innerHTML = html;
        }
    })
}


function getArticle() {
    var id = getQueryVariable("id");
    var article = document.getElementById("article");
    var count = getCountArticle();
    $.ajax({
        url: 'control/article.php',
        type: 'post',
        data: {
            "id": id
        },
        dataType: 'json',
        async: false,
        success: function (result) {
            var jsonData = eval(result);
            var html = '';
            html += '<header>';
            html += '<h1>' + jsonData.title + '</h1>';
            html += '<p>' + jsonData.time + '</p>';
            html += '</header>';
            html += '<section id="content"><pre style="white-space: pre-line;">' + jsonData.content + '</pre></section>';
            article.innerHTML = html;
        }
    });
}